package com.ask.service.Impl;

import com.ask.context.BaseContext;
import com.ask.dto.ShoppingCartDTO;
import com.ask.entity.Dish;
import com.ask.entity.SetMeal;
import com.ask.entity.ShoppingCart;
import com.ask.mapper.DishMapper;
import com.ask.mapper.SetMealDishMapper;
import com.ask.mapper.SetMealMapper;
import com.ask.mapper.ShoppingCartMapper;
import com.ask.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private SetMealMapper setMealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 获取当前用户的id
        Long userId = BaseContext.getCurrentId();

        // 新建购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);

        // 获取user的购物车列表(list by userId, dishId, setmealId)
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        // 购物车中已经存在菜品或套餐
        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            ShoppingCart cart = shoppingCartList.get(0);
            cart.setNumber(cart.getNumber() + 1);  // 增加商品数量
            shoppingCartMapper.updateNumber(cart);  // 更新购物车中商品的数量
        }
        // 若是第一次添加该商品
        else {
            Long dishId = shoppingCart.getDishId();
            Long setmealId = shoppingCart.getSetmealId();
            // 如果新增的是菜品
            if (dishId != null) {
                Dish dish = dishMapper.getById(dishId); // 根据菜品ID获取菜品详情
                BeanUtils.copyProperties(dish, shoppingCart); // 将菜品的属性复制到购物车对象中
                shoppingCart.setAmount(dish.getPrice());   // 设置购物车中商品的价格
            }
            // 如果新增的是套餐
            else {
                SetMeal setMeal = setMealMapper.getById(setmealId); // 根据套餐ID获取套餐详情
                BeanUtils.copyProperties(setMeal, shoppingCart); // 将套餐的属性复制到购物车对象中
                shoppingCart.setAmount(setMeal.getPrice()); // 设置购物车中商品的价格
            }
            // 跟新数据库
            shoppingCart.setNumber(1); // 设置购物车中商品的数量为1
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart); // 将新购物车对象插入数据库
        }
    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart cart = new ShoppingCart();
        cart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(cart);
        return list;
    }

    /**
     * 删除购物车中的一个商品
     * @param cartDTO
     */
    @Override
    public void deleteItem(ShoppingCartDTO cartDTO) {
        // 获取购物车对象
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(cartDTO, cart);
        cart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> list = shoppingCartMapper.list(cart);
        cart  = list.get(0);
        Integer number = cart.getNumber();
        // 删除的是最后一个商品
        if (number == 1) {
            shoppingCartMapper.delete(cart.getId());
        }
        // 仅做数量的跟新
        else {
            cart.setNumber(number - 1);
            shoppingCartMapper.updateNumber(cart);
        }
    }


    @Override
    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserID(userId);
    }
}
